package org.linlinjava.litemall.db.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.db.dao.PiccPatientMapper;
import org.linlinjava.litemall.db.domain.PiccPatient;
import org.linlinjava.litemall.db.domain.PiccPatientExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PiccPatientService {
    private final Log logger = LogFactory.getLog(PiccPatientService.class);
    @Resource
    private PiccPatientMapper piccPatientMapper;

    /**
     * 根据nurseId来查询其名下的所有病人详情
     * @param nurseId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<PiccPatient> getPatientsByNurseId(Integer nurseId, Integer page, Integer limit, String sort, String order){
        PiccPatientExample piccPatientExample = new PiccPatientExample();
        PiccPatientExample.Criteria criteria = piccPatientExample.createCriteria();
        // if (nurseId != 0) criteria.andNurseIdEqualTo(nurseId);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            piccPatientExample.setOrderByClause(sort + " " + order);
        }
//        if (page!=0 && limit!=0){ PageHelper.startPage(page, limit);}

        return piccPatientMapper.selectByExample(piccPatientExample);
    }

    /**
     * 返回病人详情，根据id
     * @param id
     * @return
     */
    public PiccPatient getDetailById(Integer id){
        return piccPatientMapper.selectByPrimaryKey(id);
    }

    /**
     * 返回病人详情，根据userId
     * @param userId
     * @return
     */
    public PiccPatient getDetailByUserId(Integer userId){
        PiccPatientExample piccPatientExample = new PiccPatientExample();
        PiccPatientExample.Criteria criteria = piccPatientExample.createCriteria();
        criteria.andUserIdEqualTo(userId);

        return piccPatientMapper.selectByExample(piccPatientExample).get(0);
    }

    /**
     *  添加新的病人
     * @param record
     * @return
     */
    public PiccPatient add(PiccPatient record){
        logger.debug(record);
        piccPatientMapper.insertSelective(record);
        return record;
    }

    public PiccPatient update(PiccPatient record){
        piccPatientMapper.updateByPrimaryKey(record);
        return record;
    }

    public int count(int nurseId) {
        PiccPatientExample example = new PiccPatientExample();
        if (nurseId > 0)
            example.or().andNurseIdEqualTo(nurseId);
        return (int) piccPatientMapper.countByExample(example);
    }
}
